package com.erdos.hyt.mall.controller;/**
 * @auth Liurong
 * @date 2017/4/5.
 */

import com.erdos.hyt.base.model.HytResponse;
import com.erdos.hyt.base.model.ResponseResultHandler;
import com.erdos.hyt.mall.model.MallOrderDetail;
import com.erdos.hyt.mall.model.vo.MallOrderDetailVo;
import com.erdos.hyt.mall.service.MallConsigneeAddressService;
import com.erdos.hyt.mall.service.MallOrderDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品订单Controller
 *
 * @author liurong
 * @create 2017/4/5
 **/
@Api(value = "mall API")
@RestController
@RequestMapping(value = "/mall")
public class MallOrderDetailController {

    @Autowired
    private MallOrderDetailService mallOrderDetailService;

    @Autowired
    private MallConsigneeAddressService mallConsigneeAddressService;

    @ApiOperation(value = "商品下单（第一步：选择产品分类、数量）",notes = "商品下单",httpMethod = "POST",response = HytResponse.class)
    @RequestMapping(value = "/buyGoods",method = {RequestMethod.POST},produces ="application/json;charset=UTF-8")
    public  HytResponse buyGoods(
            @ApiParam(value = "用户ID",required = true)@RequestParam(value = "userId",required =  true)
            Integer userId,
            @ApiParam(value = "库存ID",required = true)@RequestParam(value = "skuId",required =  true)
            Integer skuId,
            @ApiParam(value = "购买数量",required = true)@RequestParam(value = "num",required =  true)
            Integer num){
        //地址信息
        //MallConsigneeAddress mallConsigneeAddress=mallConsigneeAddressService.getLatelyAddress(userId);
        //商品信息（无ID的订单信息）
        MallOrderDetail mallOrderDetail=mallOrderDetailService.generateBaseOrder(userId, skuId, num);
        //用户信息
        // BaseUser baseUser=appUserService.getUserById(userId);
        //优惠券信息
        HytResponse hytResponse= ResponseResultHandler.handleSuccess(mallOrderDetail);
        return  hytResponse;
    }

    @ApiOperation(value = "提交订单（第二步：确定收货信息、数量、发票信息）",notes = "提交订单",httpMethod = "POST",response = HytResponse.class)
    @RequestMapping(value = "/submitOrder",method = {RequestMethod.POST},produces ="application/json;charset=UTF-8")
    public  HytResponse submitOrder(
            @ApiParam(value = "地址ID(添加地址时必须提交)",required = false)@RequestParam(value = "addressId",required =  false)
            Integer  addressId,
            @ApiParam(value = "订单ID、最终数量、发票类型、发票抬头、发票内容、备注",required = true)
            @RequestBody(required =  true)
            MallOrderDetailVo orderDetailVo){
        MallOrderDetail mallOrderDetail=mallOrderDetailService.updSubmitOrder(addressId, orderDetailVo);
        HytResponse hytResponse= ResponseResultHandler.handleSuccess(mallOrderDetail);
        return  hytResponse;
    }
}
